home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / emacs-complete / fsf / emacs / lisp / faces.el < prev    next >
Lisp/Scheme  |  1994-08-10  |  36KB  |  958 lines

  1. ;;; faces.el --- Lisp interface to the c "face" structure
  2.  
  3. ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; This file is part of GNU Emacs.
  6.  
  7. ;; GNU Emacs is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2, or (at your option)
  10. ;; any later version.
  11.  
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;; GNU General Public License for more details.
  16.  
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  19. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. ;;; Commentary:
  22.  
  23. ;; Mostly derived from Lucid.
  24.  
  25. ;;; Code:
  26.  
  27.  
  28. ;;;; Functions for manipulating face vectors.
  29.  
  30. ;;; A face vector is a vector of the form:
  31. ;;;    [face NAME ID FONT FOREGROUND BACKGROUND BACKGROUND-PIXMAP UNDERLINE]
  32.  
  33. ;;; Type checkers.
  34. (defsubst internal-facep (x)
  35.   (and (vectorp x) (= (length x) 8) (eq (aref x 0) 'face)))
  36.  
  37. (defmacro internal-check-face (face)
  38.   (` (while (not (internal-facep (, face)))
  39.        (setq (, face) (signal 'wrong-type-argument (list 'internal-facep (, face)))))))
  40.  
  41. ;;; Accessors.
  42. (defsubst face-name (face)
  43.   "Return the name of face FACE."
  44.   (aref (internal-get-face face) 1))
  45.  
  46. (defsubst face-id (face)
  47.   "Return the internal ID number of face FACE."
  48.   (aref (internal-get-face face) 2))
  49.  
  50. (defsubst face-font (face &optional frame)
  51.   "Return the font name of face FACE, or nil if it is unspecified.
  52. If the optional argument FRAME is given, report on face FACE in that frame.
  53. If FRAME is t, report on the defaults for face FACE (for new frames).
  54.   The font default for a face is either nil, or a list
  55.   of the form (bold), (italic) or (bold italic).
  56. If FRAME is omitted or nil, use the selected frame."
  57.   (aref (internal-get-face face frame) 3))
  58.  
  59. (defsubst face-foreground (face &optional frame)
  60.   "Return the foreground color name of face FACE, or nil if unspecified.
  61. If the optional argument FRAME is given, report on face FACE in that frame.
  62. If FRAME is t, report on the defaults for face FACE (for new frames).
  63. If FRAME is omitted or nil, use the selected frame."
  64.   (aref (internal-get-face face frame) 4))
  65.  
  66. (defsubst face-background (face &optional frame)
  67.   "Return the background color name of face FACE, or nil if unspecified.
  68. If the optional argument FRAME is given, report on face FACE in that frame.
  69. If FRAME is t, report on the defaults for face FACE (for new frames).
  70. If FRAME is omitted or nil, use the selected frame."
  71.   (aref (internal-get-face face frame) 5))
  72.  
  73. ;;(defsubst face-background-pixmap (face &optional frame)
  74. ;; "Return the background pixmap name of face FACE, or nil if unspecified.
  75. ;;If the optional argument FRAME is given, report on face FACE in that frame.
  76. ;;Otherwise report on the defaults for face FACE (for new frames)."
  77. ;; (aref (internal-get-face face frame) 6))
  78.  
  79. (defsubst face-underline-p (face &optional frame)
  80.  "Return t if face FACE is underlined.
  81. If the optional argument FRAME is given, report on face FACE in that frame.
  82. If FRAME is t, report on the defaults for face FACE (for new frames).
  83. If FRAME is omitted or nil, use the selected frame."
  84.  (aref (internal-get-face face frame) 7))
  85.  
  86.  
  87. ;;; Mutators.
  88.  
  89. (defsubst set-face-font (face font &optional frame)
  90.   "Change the font of face FACE to FONT (a string).
  91. If the optional FRAME argument is provided, change only
  92. in that frame; otherwise change each frame."
  93.   (interactive (internal-face-interactive "font"))
  94.   (if (stringp font) (setq font (x-resolve-font-name font face frame)))
  95.   (internal-set-face-1 face 'font font 3 frame))
  96.  
  97. (defsubst set-face-foreground (face color &optional frame)
  98.   "Change the foreground color of face FACE to COLOR (a string).
  99. If the optional FRAME argument is provided, change only
  100. in that frame; otherwise change each frame."
  101.   (interactive (internal-face-interactive "foreground"))
  102.   (internal-set-face-1 face 'foreground color 4 frame))
  103.  
  104. (defsubst set-face-background (face color &optional frame)
  105.   "Change the background color of face FACE to COLOR (a string).
  106. If the optional FRAME argument is provided, change only
  107. in that frame; otherwise change each frame."
  108.   (interactive (internal-face-interactive "background"))
  109.   (internal-set-face-1 face 'background color 5 frame))
  110.  
  111. ;;(defsubst set-face-background-pixmap (face name &optional frame)
  112. ;;  "Change the background pixmap of face FACE to PIXMAP.
  113. ;;PIXMAP should be a string, the name of a file of pixmap data.
  114. ;;The directories listed in the `x-bitmap-file-path' variable are searched.
  115.  
  116. ;;Alternatively, PIXMAP may be a list of the form (WIDTH HEIGHT DATA)
  117. ;;where WIDTH and HEIGHT are the size in pixels,
  118. ;;and DATA is a string, containing the raw bits of the bitmap.  
  119.  
  120. ;;If the optional FRAME argument is provided, change only
  121. ;;in that frame; otherwise change each frame."
  122. ;;  (interactive (internal-face-interactive "background-pixmap"))
  123. ;;  (internal-set-face-1 face 'background-pixmap name 6 frame))
  124.  
  125. (defsubst set-face-underline-p (face underline-p &optional frame)
  126.   "Specify whether face FACE is underlined.  (Yes if UNDERLINE-P is non-nil.)
  127. If the optional FRAME argument is provided, change only
  128. in that frame; otherwise change each frame."
  129.   (interactive (internal-face-interactive "underline-p" "underlined"))
  130.   (internal-set-face-1 face 'underline underline-p 7 frame))
  131.  
  132.  
  133. ;;;; Associating face names (symbols) with their face vectors.
  134.  
  135. (defvar global-face-data nil
  136.   "Internal data for face support functions.  Not for external use.
  137. This is an alist associating face names with the default values for
  138. their parameters.  Newly created frames get their data from here.")
  139.  
  140. (defun face-list ()
  141.   "Returns a list of all defined face names."
  142.   (mapcar 'car global-face-data))
  143.  
  144. (defun internal-find-face (name &optional frame)
  145.   "Retrieve the face named NAME.  Return nil if there is no such face.
  146. If the optional argument FRAME is given, this gets the face NAME for
  147. that frame; otherwise, it uses the selected frame.
  148. If FRAME is the symbol t, then the global, non-frame face is returned.
  149. If NAME is already a face, it is simply returned."
  150.   (if (and (eq frame t) (not (symbolp name)))
  151.       (setq name (face-name name)))
  152.   (if (symbolp name)
  153.       (cdr (assq name
  154.          (if (eq frame t)
  155.              global-face-data
  156.            (frame-face-alist (or frame (selected-frame))))))
  157.     (internal-check-face name)
  158.     name))
  159.  
  160. (defun internal-get-face (name &optional frame)
  161.   "Retrieve the face named NAME; error if there is none.
  162. If the optional argument FRAME is given, this gets the face NAME for
  163. that frame; otherwise, it uses the selected frame.
  164. If FRAME is the symbol t, then the global, non-frame face is returned.
  165. If NAME is already a face, it is simply returned."
  166.   (or (internal-find-face name frame)
  167.       (internal-check-face name)))
  168.  
  169.  
  170. (defun internal-set-face-1 (face name value index frame)
  171.   (let ((inhibit-quit t))
  172.     (if (null frame)
  173.     (let ((frames (frame-list)))
  174.       (while frames
  175.         (internal-set-face-1 (face-name face) name value index (car frames))
  176.         (setq frames (cdr frames)))
  177.       (aset (internal-get-face (if (symbolp face) face (face-name face)) t)
  178.         index value)
  179.       value)
  180.       (or (eq frame t)
  181.       (set-face-attribute-internal (face-id face) name value frame))
  182.       (aset (internal-get-face face frame) index value))))
  183.  
  184.  
  185. (defun read-face-name (prompt)
  186.   (let (face)
  187.     (while (= (length face) 0)
  188.       (setq face (completing-read prompt
  189.                   (mapcar '(lambda (x) (list (symbol-name x)))
  190.                       (face-list))
  191.                   nil t)))
  192.     (intern face)))
  193.  
  194. (defun internal-face-interactive (what &optional bool)
  195.   (let* ((fn (intern (concat "face-" what)))
  196.      (prompt (concat "Set " what " of face"))
  197.      (face (read-face-name (concat prompt ": ")))
  198.      (default (if (fboundp fn)
  199.               (or (funcall fn face (selected-frame))
  200.               (funcall fn 'default (selected-frame)))))
  201.      (value (if bool
  202.             (y-or-n-p (concat "Should face " (symbol-name face)
  203.                       " be " bool "? "))
  204.           (read-string (concat prompt " " (symbol-name face) " to: ")
  205.                    default))))
  206.     (list face (if (equal value "") nil value))))
  207.  
  208.  
  209.  
  210. (defun make-face (name)
  211.   "Define a new FACE on all frames.  
  212. You can modify the font, color, etc of this face with the set-face- functions.
  213. If the face already exists, it is unmodified."
  214.   (interactive "SMake face: ")
  215.   (or (internal-find-face name)
  216.       (let ((face (make-vector 8 nil)))
  217.     (aset face 0 'face)
  218.     (aset face 1 name)
  219.     (let* ((frames (frame-list))
  220.            (inhibit-quit t)
  221.            (id (internal-next-face-id)))
  222.       (make-face-internal id)
  223.       (aset face 2 id)
  224.       (while frames
  225.         (set-frame-face-alist (car frames)
  226.                   (cons (cons name (copy-sequence face))
  227.                     (frame-face-alist (car frames))))
  228.         (setq frames (cdr frames)))
  229.       (setq global-face-data (cons (cons name face) global-face-data)))
  230.     ;; when making a face after frames already exist
  231.     (if (eq window-system 'x)
  232.         (make-face-x-resource-internal face))
  233.     face))
  234.   name)
  235.  
  236. ;; Fill in a face by default based on X resources, for all existing frames.
  237. ;; This has to be done when a new face is made.
  238. (defun make-face-x-resource-internal (face &optional frame set-anyway)
  239.   (cond ((null frame)
  240.      (let ((frames (frame-list)))
  241.        (while frames
  242.          (if (eq (framep (car frames)) 'x)
  243.          (make-face-x-resource-internal (face-name face)
  244.                         (car frames) set-anyway))
  245.          (setq frames (cdr frames)))))
  246.     (t
  247.      (setq face (internal-get-face (face-name face) frame))
  248.      ;;
  249.      ;; These are things like "attributeForeground" instead of simply
  250.      ;; "foreground" because people tend to do things like "*foreground",
  251.      ;; which would cause all faces to be fully qualified, making faces
  252.      ;; inherit attributes in a non-useful way.  So we've made them slightly
  253.      ;; less obvious to specify in order to make them work correctly in
  254.      ;; more random environments.
  255.      ;;
  256.      ;; I think these should be called "face.faceForeground" instead of
  257.      ;; "face.attributeForeground", but they're the way they are for
  258.      ;; hysterical reasons.
  259.      ;; 
  260.      (let* ((name (symbol-name (face-name face)))
  261.         (fn  (or (x-get-resource (concat name ".attributeFont")
  262.                      "Face.AttributeFont")
  263.              (and set-anyway (face-font face))))
  264.         (fg  (or (x-get-resource (concat name ".attributeForeground")
  265.                      "Face.AttributeForeground")
  266.              (and set-anyway (face-foreground face))))
  267.         (bg  (or (x-get-resource (concat name ".attributeBackground")
  268.                      "Face.AttributeBackground")
  269.              (and set-anyway (face-background face))))
  270. ;;        (bgp (or (x-get-resource (concat name ".attributeBackgroundPixmap")
  271. ;;                     "Face.AttributeBackgroundPixmap")
  272. ;;             (and set-anyway (face-background-pixmap face))))
  273.         (ulp (let ((resource (x-get-resource
  274.                       (concat name ".attributeUnderline")
  275.                       "Face.AttributeUnderline")))
  276.                (if resource
  277.                (member (downcase resource) '("on" "true"))
  278.              (and set-anyway (face-underline-p face)))))
  279.         )
  280.        (if fn
  281.            (condition-case ()
  282.            (set-face-font face fn frame)
  283.          (error (message "font `%s' not found for face `%s'" fn name))))
  284.        (if fg
  285.            (condition-case ()
  286.            (set-face-foreground face fg frame)
  287.          (error (message "color `%s' not allocated for face `%s'" fg name))))
  288.        (if bg
  289.            (condition-case ()
  290.            (set-face-background face bg frame)
  291.          (error (message "color `%s' not allocated for face `%s'" bg name))))
  292. ;;       (if bgp
  293. ;;           (condition-case ()
  294. ;;           (set-face-background-pixmap face bgp frame)
  295. ;;         (error (message "pixmap `%s' not found for face `%s'" bgp name))))
  296.        (if (or ulp set-anyway)
  297.            (set-face-underline-p face ulp frame))
  298.        )))
  299.   face)
  300.  
  301. (defun copy-face (old-face new-face &optional frame new-frame)
  302.   "Define a face just like OLD-FACE, with name NEW-FACE.
  303. If NEW-FACE already exists as a face, it is modified to be like OLD-FACE.
  304. If it doesn't already exist, it is created.
  305.  
  306. If the optional argument FRAME is given as a frame,
  307. NEW-FACE is changed on FRAME only.
  308. If FRAME is t, the frame-independent default specification for OLD-FACE
  309. is copied to NEW-FACE.
  310. If FRAME is nil, copying is done for the frame-independent defaults
  311. and for each existing frame.
  312. If the optional fourth argument NEW-FRAME is given, 
  313. copy the information from face OLD-FACE on frame FRAME
  314. to NEW-FACE on frame NEW-FRAME."
  315.   (or new-frame (setq new-frame frame))
  316.   (let ((inhibit-quit t))
  317.     (if (null frame)
  318.     (let ((frames (frame-list)))
  319.       (while frames
  320.         (copy-face old-face new-face (car frames))
  321.         (setq frames (cdr frames)))
  322.       (copy-face old-face new-face t))
  323.       (setq old-face (internal-get-face old-face frame))
  324.       (setq new-face (or (internal-find-face new-face new-frame)
  325.              (make-face new-face)))
  326.       (condition-case nil
  327.       ;; A face that has a global symbolic font modifier such as `bold'
  328.       ;; might legitimately get an error here.
  329.       ;; Use the frame's default font in that case.
  330.       (set-face-font new-face (face-font old-face frame) new-frame)
  331.     (error
  332.      (set-face-font new-face nil new-frame)))
  333.       (set-face-foreground new-face (face-foreground old-face frame) new-frame)
  334.       (set-face-background new-face (face-background old-face frame) new-frame)
  335. ;;;      (set-face-background-pixmap
  336. ;;;       new-face (face-background-pixmap old-face frame) new-frame)
  337.       (set-face-underline-p new-face (face-underline-p old-face frame)
  338.                 new-frame))
  339.     new-face))
  340.  
  341. (defun face-equal (face1 face2 &optional frame)
  342.   "True if the faces FACE1 and FACE2 display in the same way."
  343.   (setq face1 (internal-get-face face1 frame)
  344.     face2 (internal-get-face face2 frame))
  345.   (and (equal (face-foreground face1 frame) (face-foreground face2 frame))
  346.        (equal (face-background face1 frame) (face-background face2 frame))
  347.        (equal (face-font face1 frame) (face-font face2 frame))
  348.        (eq (face-underline-p face1 frame) (face-underline-p face2 frame))
  349. ;;       (equal (face-background-pixmap face1 frame)
  350. ;;          (face-background-pixmap face2 frame))
  351.        ))
  352.  
  353. (defun face-differs-from-default-p (face &optional frame)
  354.   "True if face FACE displays differently from the default face, on FRAME.
  355. A face is considered to be ``the same'' as the default face if it is 
  356. actually specified in the same way (equivalent fonts, etc) or if it is 
  357. fully unspecified, and thus inherits the attributes of any face it 
  358. is displayed on top of."
  359.   (let ((default (internal-get-face 'default frame)))
  360.     (setq face (internal-get-face face frame))
  361.     (not (and (or (equal (face-foreground default frame)
  362.              (face-foreground face frame))
  363.           (null (face-foreground face frame)))
  364.           (or (equal (face-background default frame)
  365.              (face-background face frame))
  366.           (null (face-background face frame)))
  367.           (or (equal (face-font default frame) (face-font face frame))
  368.           (null (face-font face frame)))
  369. ;;;          (or (equal (face-background-pixmap default frame)
  370. ;;;             (face-background-pixmap face frame))
  371. ;;;          (null (face-background-pixmap face frame)))
  372.           (equal (face-underline-p default frame)
  373.              (face-underline-p face frame))
  374.           ))))
  375.  
  376.  
  377. (defun invert-face (face &optional frame)
  378.   "Swap the foreground and background colors of face FACE.
  379. If the face doesn't specify both foreground and background, then
  380. set its foreground and background to the default background and foreground."
  381.   (interactive (list (read-face-name "Invert face: ")))
  382.   (setq face (internal-get-face face frame))
  383.   (let ((fg (face-foreground face frame))
  384.     (bg (face-background face frame)))
  385.     (if (or fg bg)
  386.     (progn
  387.       (set-face-foreground face bg frame)
  388.       (set-face-background face fg frame))
  389.       (set-face-foreground face (or (face-background 'default frame)
  390.                     (cdr (assq 'background-color (frame-parameters frame))))
  391.                frame)
  392.       (set-face-background face (or (face-foreground 'default frame)
  393.                     (cdr (assq 'foreground-color (frame-parameters frame))))
  394.                frame)))
  395.   face)
  396.  
  397.  
  398. (defun internal-try-face-font (face font &optional frame)
  399.   "Like set-face-font, but returns nil on failure instead of an error."
  400.   (condition-case ()
  401.       (set-face-font face font frame)
  402.     (error nil)))
  403.  
  404. ;; Manipulating font names.
  405.  
  406. (defconst x-font-regexp nil)
  407. (defconst x-font-regexp-head nil)
  408. (defconst x-font-regexp-weight nil)
  409. (defconst x-font-regexp-slant nil)
  410.  
  411. ;;; Regexps matching font names in "Host Portable Character Representation."
  412. ;;;
  413. (let ((-         "[-?]")
  414.       (foundry        "[^-]+")
  415.       (family         "[^-]+")
  416.       (weight        "\\(bold\\|demibold\\|medium\\)")        ; 1
  417. ;     (weight\?        "\\(\\*\\|bold\\|demibold\\|medium\\|\\)")    ; 1
  418.       (weight\?        "\\([^-]*\\)")                    ; 1
  419.       (slant        "\\([ior]\\)")                    ; 2
  420. ;     (slant\?        "\\([ior?*]?\\)")                ; 2
  421.       (slant\?        "\\([^-]?\\)")                    ; 2
  422. ;     (swidth        "\\(\\*\\|normal\\|semicondensed\\|\\)")    ; 3
  423.       (swidth        "\\([^-]*\\)")                    ; 3
  424. ;     (adstyle        "\\(\\*\\|sans\\|\\)")                ; 4
  425.       (adstyle        "[^-]*")                    ; 4
  426.       (pixelsize    "[0-9]+")
  427.       (pointsize    "[0-9][0-9]+")
  428.       (resx        "[0-9][0-9]+")
  429.       (resy        "[0-9][0-9]+")
  430.       (spacing        "[cmp?*]")
  431.       (avgwidth        "[0-9]+")
  432.       (registry        "[^-]+")
  433.       (encoding        "[^-]+")
  434.       )
  435.   (setq x-font-regexp
  436.     (concat "\\`\\*?[-?*]"
  437.         foundry - family - weight\? - slant\? - swidth - adstyle -
  438.         pixelsize - pointsize - resx - resy - spacing - registry -
  439.         encoding "[-?*]\\*?\\'"
  440.         ))
  441.   (setq x-font-regexp-head
  442.     (concat "\\`[-?*]" foundry - family - weight\? - slant\?
  443.         "\\([-*?]\\|\\'\\)"))
  444.   (setq x-font-regexp-slant (concat - slant -))
  445.   (setq x-font-regexp-weight (concat - weight -))
  446.   nil)        
  447.  
  448. (defun x-resolve-font-name (pattern &optional face frame)
  449.   "Return a font name matching PATTERN.
  450. All wildcards in PATTERN become substantiated.
  451. If PATTERN is nil, return the name of the frame's base font, which never
  452. contains wildcards.
  453. Given optional arguments FACE and FRAME, try to return a font which is
  454. also the same size as FACE on FRAME."
  455.   (or (symbolp face)
  456.       (setq face (face-name face)))
  457.   (and (eq frame t)
  458.        (setq frame nil))
  459.   (if pattern
  460.       ;; Note that x-list-fonts has code to handle a face with nil as its font.
  461.       (let ((fonts (x-list-fonts pattern face frame)))
  462.     (or fonts
  463.         (if face
  464.         (error "No fonts matching pattern are the same size as `%s'"
  465.                face)
  466.           (error "No fonts match `%s'" pattern)))
  467.     (car fonts))
  468.     (cdr (assq 'font (frame-parameters (selected-frame))))))
  469.  
  470. (defun x-frob-font-weight (font which)
  471.   (if (or (string-match x-font-regexp font)
  472.       (string-match x-font-regexp-head font)
  473.       (string-match x-font-regexp-weight font))
  474.       (concat (substring font 0 (match-beginning 1)) which
  475.           (substring font (match-end 1)))
  476.     nil))
  477.  
  478. (defun x-frob-font-slant (font which)
  479.   (cond ((or (string-match x-font-regexp font)
  480.          (string-match x-font-regexp-head font))
  481.      (concat (substring font 0 (match-beginning 2)) which
  482.          (substring font (match-end 2))))
  483.     ((string-match x-font-regexp-slant font)
  484.      (concat (substring font 0 (match-beginning 1)) which
  485.          (substring font (match-end 1))))
  486.     (t nil)))
  487.  
  488.  
  489. (defun x-make-font-bold (font)
  490.   "Given an X font specification, make a bold version of it.
  491. If that can't be done, return nil."
  492.   (x-frob-font-weight font "bold"))
  493.  
  494. (defun x-make-font-demibold (font)
  495.   "Given an X font specification, make a demibold version of it.
  496. If that can't be done, return nil."
  497.   (x-frob-font-weight font "demibold"))
  498.  
  499. (defun x-make-font-unbold (font)
  500.   "Given an X font specification, make a non-bold version of it.
  501. If that can't be done, return nil."
  502.   (x-frob-font-weight font "medium"))
  503.  
  504. (defun x-make-font-italic (font)
  505.   "Given an X font specification, make an italic version of it.
  506. If that can't be done, return nil."
  507.   (x-frob-font-slant font "i"))
  508.  
  509. (defun x-make-font-oblique (font) ; you say tomayto...
  510.   "Given an X font specification, make an oblique version of it.
  511. If that can't be done, return nil."
  512.   (x-frob-font-slant font "o"))
  513.  
  514. (defun x-make-font-unitalic (font)
  515.   "Given an X font specification, make a non-italic version of it.
  516. If that can't be done, return nil."
  517.   (x-frob-font-slant font "r"))
  518.  
  519. ;;; non-X-specific interface
  520.  
  521. (defun make-face-bold (face &optional frame noerror)
  522.   "Make the font of the given face be bold, if possible.  
  523. If NOERROR is non-nil, return nil on failure."
  524.   (interactive (list (read-face-name "Make which face bold: ")))
  525.   (if (and (eq frame t) (listp (face-font face t)))
  526.       (set-face-font face (if (memq 'italic (face-font face t))
  527.                   '(bold italic) '(bold))
  528.              t)
  529.     (let ((ofont (face-font face frame))
  530.       font)
  531.       (if (null frame)
  532.       (let ((frames (frame-list)))
  533.         ;; Make this face bold in global-face-data.
  534.         (make-face-bold face t noerror)
  535.         ;; Make this face bold in each frame.
  536.         (while frames
  537.           (make-face-bold face (car frames) noerror)
  538.           (setq frames (cdr frames))))
  539.     (setq face (internal-get-face face frame))
  540.     (setq font (or (face-font face frame)
  541.                (face-font face t)))
  542.     (if (listp font)
  543.         (setq font nil))
  544.     (setq font (or font
  545.                (face-font 'default frame)
  546.                (cdr (assq 'font (frame-parameters frame)))))
  547.     (make-face-bold-internal face frame font))
  548.       (or (not (equal ofont (face-font face)))
  549.       (and (not noerror)
  550.            (error "No bold version of %S" font))))))
  551.  
  552. (defun make-face-bold-internal (face frame font)
  553.   (let (f2)
  554.     (or (and (setq f2 (x-make-font-bold font))
  555.          (internal-try-face-font face f2 frame))
  556.     (and (setq f2 (x-make-font-demibold font))
  557.          (internal-try-face-font face f2 frame)))))
  558.  
  559. (defun make-face-italic (face &optional frame noerror)
  560.   "Make the font of the given face be italic, if possible.  
  561. If NOERROR is non-nil, return nil on failure."
  562.   (interactive (list (read-face-name "Make which face italic: ")))
  563.   (if (and (eq frame t) (listp (face-font face t)))
  564.       (set-face-font face (if (memq 'bold (face-font face t))
  565.                   '(bold italic) '(italic))
  566.              t)
  567.     (let ((ofont (face-font face frame))
  568.       font)
  569.       (if (null frame)
  570.       (let ((frames (frame-list)))
  571.         ;; Make this face italic in global-face-data.
  572.         (make-face-italic face t noerror)
  573.         ;; Make this face italic in each frame.
  574.         (while frames
  575.           (make-face-italic face (car frames) noerror)
  576.           (setq frames (cdr frames))))
  577.     (setq face (internal-get-face face frame))
  578.     (setq font (or (face-font face frame)
  579.                (face-font face t)))
  580.     (if (listp font)
  581.         (setq font nil))
  582.     (setq font (or font
  583.                (face-font 'default frame)
  584.                (cdr (assq 'font (frame-parameters frame)))))
  585.     (make-face-italic-internal face frame font))
  586.       (or (not (equal ofont (face-font face)))
  587.       (and (not noerror)
  588.            (error "No italic version of %S" font))))))
  589.  
  590. (defun make-face-italic-internal (face frame font)
  591.   (let (f2)
  592.     (or (and (setq f2 (x-make-font-italic font))
  593.          (internal-try-face-font face f2 frame))
  594.     (and (setq f2 (x-make-font-oblique font))
  595.          (internal-try-face-font face f2 frame)))))
  596.  
  597. (defun make-face-bold-italic (face &optional frame noerror)
  598.   "Make the font of the given face be bold and italic, if possible.  
  599. If NOERROR is non-nil, return nil on failure."
  600.   (interactive (list (read-face-name "Make which face bold-italic: ")))
  601.   (if (and (eq frame t) (listp (face-font face t)))
  602.       (set-face-font face '(bold italic) t)
  603.     (let ((ofont (face-font face frame))
  604.       font)
  605.       (if (null frame)
  606.       (let ((frames (frame-list)))
  607.         ;; Make this face bold-italic in global-face-data.
  608.         (make-face-bold-italic face t noerror)
  609.         ;; Make this face bold in each frame.
  610.         (while frames
  611.           (make-face-bold-italic face (car frames) noerror)
  612.           (setq frames (cdr frames))))
  613.     (setq face (internal-get-face face frame))
  614.     (setq font (or (face-font face frame)
  615.                (face-font face t)))
  616.     (if (listp font)
  617.         (setq font nil))
  618.     (setq font (or font
  619.                (face-font 'default frame)
  620.                (cdr (assq 'font (frame-parameters frame)))))
  621.     (make-face-bold-italic-internal face frame font))
  622.       (or (not (equal ofont (face-font face)))
  623.       (and (not noerror)
  624.            (error "No bold italic version of %S" font))))))
  625.  
  626. (defun make-face-bold-italic-internal (face frame font)
  627.   (let (f2 f3)
  628.     (or (and (setq f2 (x-make-font-italic font))
  629.          (not (equal font f2))
  630.          (setq f3 (x-make-font-bold f2))
  631.          (not (equal f2 f3))
  632.          (internal-try-face-font face f3 frame))
  633.     (and (setq f2 (x-make-font-oblique font))
  634.          (not (equal font f2))
  635.          (setq f3 (x-make-font-bold f2))
  636.          (not (equal f2 f3))
  637.          (internal-try-face-font face f3 frame))
  638.     (and (setq f2 (x-make-font-italic font))
  639.          (not (equal font f2))
  640.          (setq f3 (x-make-font-demibold f2))
  641.          (not (equal f2 f3))
  642.          (internal-try-face-font face f3 frame))
  643.     (and (setq f2 (x-make-font-oblique font))
  644.          (not (equal font f2))
  645.          (setq f3 (x-make-font-demibold f2))
  646.          (not (equal f2 f3))
  647.          (internal-try-face-font face f3 frame)))))
  648.  
  649. (defun make-face-unbold (face &optional frame noerror)
  650.   "Make the font of the given face be non-bold, if possible.  
  651. If NOERROR is non-nil, return nil on failure."
  652.   (interactive (list (read-face-name "Make which face non-bold: ")))
  653.   (if (and (eq frame t) (listp (face-font face t)))
  654.       (set-face-font face (if (memq 'italic (face-font face t))
  655.                   '(italic) nil)
  656.              t)
  657.     (let ((ofont (face-font face frame))
  658.       font font1)
  659.       (if (null frame)
  660.       (let ((frames (frame-list)))
  661.         ;; Make this face unbold in global-face-data.
  662.         (make-face-unbold face t noerror)
  663.         ;; Make this face unbold in each frame.
  664.         (while frames
  665.           (make-face-unbold face (car frames) noerror)
  666.           (setq frames (cdr frames))))
  667.     (setq face (internal-get-face face frame))
  668.     (setq font1 (or (face-font face frame)
  669.             (face-font face t)))
  670.     (if (listp font1)
  671.         (setq font1 nil))
  672.     (setq font1 (or font1
  673.             (face-font 'default frame)
  674.             (cdr (assq 'font (frame-parameters frame)))))
  675.     (setq font (x-make-font-unbold font1))
  676.     (if font (internal-try-face-font face font frame)))
  677.       (or (not (equal ofont (face-font face)))
  678.       (and (not noerror)
  679.            (error "No unbold version of %S" font1))))))
  680.  
  681. (defun make-face-unitalic (face &optional frame noerror)
  682.   "Make the font of the given face be non-italic, if possible.  
  683. If NOERROR is non-nil, return nil on failure."
  684.   (interactive (list (read-face-name "Make which face non-italic: ")))
  685.   (if (and (eq frame t) (listp (face-font face t)))
  686.       (set-face-font face (if (memq 'bold (face-font face t))
  687.                   '(bold) nil)
  688.              t)
  689.     (let ((ofont (face-font face frame))
  690.       font font1)
  691.       (if (null frame)
  692.       (let ((frames (frame-list)))
  693.         ;; Make this face unitalic in global-face-data.
  694.         (make-face-unitalic face t noerror)
  695.         ;; Make this face unitalic in each frame.
  696.         (while frames
  697.           (make-face-unitalic face (car frames) noerror)
  698.           (setq frames (cdr frames))))
  699.     (setq face (internal-get-face face frame))
  700.     (setq font1 (or (face-font face frame)
  701.             (face-font face t)))
  702.     (if (listp font1)
  703.         (setq font1 nil))
  704.     (setq font1 (or font1
  705.             (face-font 'default frame)
  706.             (cdr (assq 'font (frame-parameters frame)))))
  707.     (setq font (x-make-font-unitalic font1))
  708.     (if font (internal-try-face-font face font frame)))
  709.       (or (not (equal ofont (face-font face)))
  710.       (and (not noerror)
  711.            (error "No unitalic version of %S" font1))))))
  712.  
  713. (defvar list-faces-sample-text
  714.   "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  715.   "*Text string to display as the sample text for `list-faces-display'.")
  716.  
  717. ;; The name list-faces would be more consistent, but let's avoid a conflict
  718. ;; with Lucid, which uses that name differently.
  719. (defun list-faces-display ()
  720.   "List all faces, using the same sample text in each.
  721. The sample text is a string that comes from the variable
  722. `list-faces-sample-text'.
  723.  
  724. It is possible to give a particular face name different appearances in
  725. different frames.  This command shows the appearance in the
  726. selected frame."
  727.   (interactive)
  728.   (let ((faces (sort (face-list) (function string-lessp)))
  729.     (face nil)
  730.     (frame (selected-frame))
  731.     disp-frame window)
  732.     (with-output-to-temp-buffer "*Faces*"
  733.       (save-excursion
  734.     (set-buffer standard-output)
  735.     (setq truncate-lines t)
  736.     (while faces
  737.       (setq face (car faces))
  738.       (setq faces (cdr faces))
  739.       (insert (format "%25s " (symbol-name face)))
  740.       (let ((beg (point)))
  741.         (insert list-faces-sample-text)
  742.         (insert "\n")
  743.         (put-text-property beg (1- (point)) 'face face)
  744.         ;; If the sample text has multiple lines, line up all of them.
  745.         (goto-char beg)
  746.         (forward-line 1)
  747.         (while (not (eobp))
  748.           (insert "                          ")
  749.           (forward-line 1))))
  750.     (goto-char (point-min))))
  751.     ;; If the *Faces* buffer appears in a different frame,
  752.     ;; copy all the face definitions from FRAME,
  753.     ;; so that the display will reflect the frame that was selected.
  754.     (setq window (get-buffer-window (get-buffer "*Faces*") t))
  755.     (setq disp-frame (if window (window-frame window)
  756.                (car (frame-list))))
  757.     (or (eq frame disp-frame)
  758.     (let ((faces (face-list)))
  759.       (while faces
  760.         (copy-face (car faces) (car faces) frame disp-frame)
  761.         (setq faces (cdr faces)))))))
  762.  
  763. ;;; Make the standard faces.
  764. ;;; The C code knows the default and modeline faces as faces 0 and 1,
  765. ;;; so they must be the first two faces made.
  766. (defun face-initialize ()
  767.   (make-face 'default)
  768.   (make-face 'modeline)
  769.   (make-face 'highlight)
  770.  
  771.   ;; These aren't really special in any way, but they're nice to have around.
  772.  
  773.   (make-face 'bold)
  774.   (make-face 'italic)
  775.   (make-face 'bold-italic)
  776.   (make-face 'region)
  777.   (make-face 'secondary-selection)
  778.   (make-face 'underline)
  779.  
  780.   (setq region-face (face-id 'region))
  781.  
  782.   ;; Specify the global properties of these faces
  783.   ;; so they will come out right on new frames.
  784.  
  785.   (make-face-bold 'bold t)
  786.   (make-face-italic 'italic t)
  787.   (make-face-bold-italic 'bold-italic t)
  788.  
  789.   (set-face-background 'highlight '("darkseagreen2" "green" t) t)
  790.   (set-face-background 'region '("gray" underline) t)
  791.   (set-face-background 'secondary-selection '("paleturquoise" "green" t) t)
  792.   (set-face-background 'modeline '(t) t)
  793.   (set-face-underline-p 'underline t t)
  794.  
  795.   ;; Set up the faces of all existing X Window frames
  796.   ;; from those global properties, unless already set in a given frame.
  797.  
  798.   (let ((frames (frame-list)))
  799.     (while frames
  800.       (if (eq (framep (car frames)) 'x)
  801.       (let ((frame (car frames))
  802.         (rest global-face-data))
  803.         (while rest
  804.           (let ((face (car (car rest))))
  805.         (or (face-differs-from-default-p face)
  806.             (face-fill-in face (cdr (car rest)) frame)))
  807.           (setq rest (cdr rest)))))
  808.       (setq frames (cdr frames)))))
  809.  
  810.  
  811. ;; Like x-create-frame but also set up the faces.
  812.  
  813. (defun x-create-frame-with-faces (&optional parameters)
  814.   (if (null global-face-data)
  815.       (x-create-frame parameters)
  816.     (let* ((visibility-spec (assq 'visibility parameters))
  817.        (frame (x-create-frame (cons '(visibility . nil) parameters)))
  818.        (faces (copy-alist global-face-data))
  819.        (rest faces))
  820.       (set-frame-face-alist frame faces)
  821.  
  822.       (if (cdr (or (assq 'reverse parameters)
  823.            (assq 'reverse default-frame-alist)
  824.            (let ((resource (x-get-resource "reverseVideo"
  825.                            "ReverseVideo")))
  826.              (if resource
  827.              (cons nil (member (downcase resource)
  828.                        '("on" "true")))))))
  829.       (let ((params (frame-parameters frame)))
  830.         (modify-frame-parameters
  831.          frame
  832.          (list (cons 'foreground-color (cdr (assq 'background-color params)))
  833.            (cons 'background-color (cdr (assq 'foreground-color params)))
  834.            (cons 'mouse-color (cdr (assq 'background-color params)))
  835.            (cons 'border-color (cdr (assq 'background-color params)))))
  836.         (modify-frame-parameters
  837.          frame
  838.          (list (cons 'cursor-color (cdr (assq 'background-color params)))))))
  839.  
  840.       ;; Copy the vectors that represent the faces.
  841.       ;; Also fill them in from X resources.
  842.       (while rest
  843.     (let ((global (cdr (car rest))))
  844.       (setcdr (car rest) (vector 'face
  845.                      (face-name (cdr (car rest)))
  846.                      (face-id (cdr (car rest)))
  847.                      nil nil nil nil nil))
  848.       (face-fill-in (car (car rest)) global frame))
  849.     (make-face-x-resource-internal (cdr (car rest)) frame t)
  850.     (setq rest (cdr rest)))
  851.       (if (null visibility-spec)
  852.       (make-frame-visible frame)
  853.     (modify-frame-parameters frame (list visibility-spec)))
  854.       frame)))
  855.  
  856. ;; Update a frame's faces when we change its default font.
  857. (defun frame-update-faces (frame)
  858.   (let* ((faces global-face-data)
  859.      (rest faces))
  860.     (while rest
  861.       (let* ((face (car (car rest)))
  862.          (font (face-font face t)))
  863.     (if (listp font)
  864.         (let ((bold (memq 'bold font))
  865.           (italic (memq 'italic font)))
  866.           ;; Ignore any previous (string-valued) font, it might not even
  867.           ;; be the right size anymore.
  868.           (set-face-font face nil frame)
  869.           (cond ((and bold italic)
  870.              (make-face-bold-italic face frame t))
  871.             (bold
  872.              (make-face-bold face frame t))
  873.             (italic
  874.              (make-face-italic face frame t)))))
  875.       (setq rest (cdr rest)))
  876.     frame)))
  877.  
  878. ;; Fill in the face FACE from frame-independent face data DATA.
  879. ;; DATA should be the non-frame-specific ("global") face vector
  880. ;; for the face.  FACE should be a face name or face object.
  881. ;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
  882. (defun face-fill-in (face data frame)
  883.   (condition-case nil
  884.       (let ((foreground (face-foreground data))
  885.         (background (face-background data))
  886.         (font (face-font data)))
  887.     (set-face-underline-p face (face-underline-p data) frame)
  888.     (if foreground
  889.         (face-try-color-list 'set-face-foreground
  890.                  face foreground frame))
  891.     (if background
  892.         (face-try-color-list 'set-face-background
  893.                  face background frame))
  894.     (if (listp font)
  895.         (let ((bold (memq 'bold font))
  896.           (italic (memq 'italic font)))
  897.           (cond ((and bold italic)
  898.              (make-face-bold-italic face frame))
  899.             (bold
  900.              (make-face-bold face frame))
  901.             (italic
  902.              (make-face-italic face frame))))
  903.       (if font
  904.           (set-face-font face font frame))))
  905.     (error nil)))
  906.  
  907. ;; Use FUNCTION to store a color in FACE on FRAME.
  908. ;; COLORS is either a single color or a list of colors.
  909. ;; If it is a list, try the colors one by one until one of them
  910. ;; succeeds.  We signal an error only if all the colors failed.
  911. ;; t as COLORS or as an element of COLORS means to invert the face.
  912. ;; That can't fail, so any subsequent elements after the t are ignored.
  913. (defun face-try-color-list (function face colors frame)
  914.   (if (stringp colors)
  915.       (if (or (and (not (x-display-color-p)) (not (string= colors "gray")))
  916.           (= (x-display-planes) 1))
  917.       nil
  918.     (funcall function face colors frame))
  919.     (if (eq colors t)
  920.     (invert-face face frame)
  921.       (let (done)
  922.     (while (and colors (not done))
  923.       (if (and (stringp (car colors))
  924.            (or (and (not (x-display-color-p))
  925.                 (not (string= (car colors) "gray")))
  926.                (= (x-display-planes) 1)))
  927.           nil
  928.         (if (cdr colors)
  929.         ;; If there are more colors to try, catch errors
  930.         ;; and set `done' if we succeed.
  931.         (condition-case nil
  932.             (progn
  933.               (cond ((eq (car colors) t)
  934.                  (invert-face face frame))
  935.                 ((eq (car colors) 'underline)
  936.                  (set-face-underline-p face t frame))
  937.                 (t
  938.                  (funcall function face (car colors) frame)))
  939.               (setq done t))
  940.           (error nil))
  941.           ;; If this is the last color, let the error get out if it fails.
  942.           ;; If it succeeds, we will exit anyway after this iteration.
  943.           (cond ((eq (car colors) t)
  944.              (invert-face face frame))
  945.             ((eq (car colors) 'underline)
  946.              (set-face-underline-p face t frame))
  947.             (t
  948.              (funcall function face (car colors) frame)))))
  949.       (setq colors (cdr colors)))))))
  950.  
  951. ;; If we are already using x-window frames, initialize faces for them.
  952. (if (eq (framep (selected-frame)) 'x)
  953.     (face-initialize))
  954.  
  955. (provide 'faces)
  956.  
  957. ;;; faces.el ends here
  958.